package itbour.onetouchshow.activity.version1_6;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.ViewTreeObserver;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;

import butterknife.BindView;
import itbour.onetouchshow.R;
import itbour.onetouchshow.base.mvp.MVPBaseActivity;
import itbour.onetouchshow.bean.ShowImageBean;
import itbour.onetouchshow.bean.canvas.CompoundPathsBean;
import itbour.onetouchshow.bean.canvas.PosBean;
import itbour.onetouchshow.widget.BoundView;
import itbour.onetouchshow.widget.DragPhotoView;

public class DragPhotoActivity extends MVPBaseActivity {

    private static String COMPOUNDPATH = "COMPOUNDPATH";
    private static String POSBEAN = "POSBEAN";
    private static String IMAGEBEAN = "IMAGEBEAN";
    int mOriginLeft;
    int mOriginTop;
    int mOriginHeight;
    int mOriginWidth;
    int mOriginCenterX;
    int mOriginCenterY;
    String imageurl;
    @BindView(R.id.id_root_bound)
    RelativeLayout idRootBound;
    int w;
    int h;
    int pageW;
    int pageH;
    private float mTargetHeight;
    private float mTargetWidth;
    private float mScaleX;
    private float mScaleY;
    private float mTranslationX;
    private float mTranslationY;
    private DragPhotoView dragPhotoView;

    public static Intent getLaunchIntent(Context context, ShowImageBean showImageBean, CompoundPathsBean compoundPathsBean, PosBean posBean) {
        Intent intent = new Intent(context, DragPhotoActivity.class);
        intent.putExtra(COMPOUNDPATH, compoundPathsBean);
        intent.putExtra(POSBEAN, posBean);
        intent.putExtra(IMAGEBEAN, showImageBean);
        return intent;
    }


    @Override
    protected int getLayoutId() {
        return R.layout.activity_drag_photo;
    }

    @Override
    protected void afterCreate(Bundle bundle) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().setStatusBarColor(getResources().getColor(R.color.colorPrimary));
        }
        dragPhotoView = findViewById(R.id.id_drop_view);

        dragPhotoView.setOnTapListener(new DragPhotoView.OnTapListener() {
            @Override
            public void onTap(DragPhotoView view) {
                finishWithAnimation();
            }
        });

        dragPhotoView.setOnExitListener(new DragPhotoView.OnExitListener() {
            @Override
            public void onExit(DragPhotoView view, float x, float y, float w, float h) {
                performExitAnimation(view, x, y, w, h);
            }
        });

        CompoundPathsBean compoundPathsBean = (CompoundPathsBean) getIntent().getSerializableExtra(COMPOUNDPATH);
        PosBean posBean = (PosBean) getIntent().getSerializableExtra(POSBEAN);
        ShowImageBean showImageBean = (ShowImageBean) getIntent().getSerializableExtra(IMAGEBEAN);
        mOriginLeft = showImageBean.getX();
        mOriginTop = showImageBean.getY();
        h = mOriginHeight = showImageBean.getH();
        w = mOriginWidth = showImageBean.getW();
        imageurl = showImageBean.getImageUrl();
        pageW = showImageBean.getPageW();
        pageH = showImageBean.getPageH();


//        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) idRootBound.getLayoutParams();
//        layoutParams.width = w;
//        layoutParams.height = h;


        //showBoundView(pageW, pageH, compoundPathsBean, posBean);

        dragPhotoView.getViewTreeObserver()
                .addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {

                        mOriginCenterX = mOriginLeft + mOriginWidth / 2;
                        mOriginCenterY = mOriginTop + mOriginHeight / 2;
                        int[] location = new int[2];
                        dragPhotoView.getLocationOnScreen(location);
                        mTargetHeight = (float) dragPhotoView.getHeight();
                        mTargetWidth = (float) dragPhotoView.getWidth();
                        mScaleX = (float) mOriginWidth / mTargetWidth;
                        mScaleY = (float) mOriginHeight / mTargetHeight;
                        float targetCenterX = location[0] + mTargetWidth / 2;
                        float targetCenterY = location[1] + mTargetHeight / 2;
                        mTranslationX = mOriginCenterX - targetCenterX;
                        mTranslationY = mOriginCenterY - targetCenterY;
                        dragPhotoView.setTranslationX(mTranslationX);
                        dragPhotoView.setTranslationY(mTranslationY);
                        dragPhotoView.setScaleX(mScaleX);
                        dragPhotoView.setScaleY(mScaleY);
                        performEnterAnimation();
                        dragPhotoView.setMinScale(mScaleX);

                        Glide.with(DragPhotoActivity.this).applyDefaultRequestOptions(new RequestOptions().apply(new RequestOptions().skipMemoryCache(true)).placeholder(R.drawable.place_icon).fitCenter()).load(imageurl).into(dragPhotoView);

                    }
                });
    }


    public void showBoundView(int pageW, int pageH, CompoundPathsBean compoundPathsBean, PosBean posBean) {

        //确定当前选中的位置
        // int pageWidth = pageW;
        //int pageHeight = pageH;
        float scaleW = w * 1.0f / pageW;
        float scaleH = h * 1.0f / pageH;

        float w = compoundPathsBean.getW() * scaleW;
        float h = compoundPathsBean.getH() * scaleH;
        float x = posBean.getX() * scaleW;
        float y = posBean.getY() * scaleH;
        BoundView pathsView = new BoundView(this);
        pathsView.setId(R.id.video_edit_bound);
        pathsView.setData(compoundPathsBean, (int) w, (int) h);

        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams((int) w, (int) h);
        lp.leftMargin = (int) x;
        lp.topMargin = (int) y;
        idRootBound.addView(pathsView, lp);
    }


    /**
     * ===================================================================================
     * <p>
     * 底下是低版本"共享元素"实现   不需要过分关心  如有需要 可作为参考.
     * <p>
     * Code  under is shared transitions in all android versions implementation
     */
    private void performExitAnimation(final DragPhotoView view, float x, float y, float w, float h) {
        view.finishAnimationCallBack();
        float viewX = mTargetWidth / 2 + x - mTargetWidth * mScaleX / 2;
        float viewY = mTargetHeight / 2 + y - mTargetHeight * mScaleY / 2;
        view.setX(viewX);
        view.setY(viewY);

        float centerX = view.getX() + mOriginWidth / 2;
        float centerY = view.getY() + mOriginHeight / 2;

        float translateX = mOriginCenterX - centerX;
        float translateY = mOriginCenterY - centerY;


        ValueAnimator translateXAnimator = ValueAnimator.ofFloat(view.getX(), view.getX() + translateX);
        translateXAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                view.setX((Float) valueAnimator.getAnimatedValue());
            }
        });
        translateXAnimator.setDuration(300);
        translateXAnimator.start();
        ValueAnimator translateYAnimator = ValueAnimator.ofFloat(view.getY(), view.getY() + translateY);
        translateYAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                view.setY((Float) valueAnimator.getAnimatedValue());
            }
        });
        translateYAnimator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animator) {

            }

            @Override
            public void onAnimationEnd(Animator animator) {
                animator.removeAllListeners();
                finish();
                overridePendingTransition(0, 0);
            }

            @Override
            public void onAnimationCancel(Animator animator) {

            }

            @Override
            public void onAnimationRepeat(Animator animator) {

            }
        });
        translateYAnimator.setDuration(300);
        translateYAnimator.start();
    }

    private void finishWithAnimation() {

        ValueAnimator translateXAnimator = ValueAnimator.ofFloat(0, mTranslationX);
        translateXAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                dragPhotoView.setX((Float) valueAnimator.getAnimatedValue());
            }
        });
        translateXAnimator.setDuration(300);
        translateXAnimator.start();

        ValueAnimator translateYAnimator = ValueAnimator.ofFloat(0, mTranslationY);
        translateYAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                dragPhotoView.setY((Float) valueAnimator.getAnimatedValue());
            }
        });
        translateYAnimator.setDuration(300);
        translateYAnimator.start();

        ValueAnimator scaleYAnimator = ValueAnimator.ofFloat(1, mScaleY);
        scaleYAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                dragPhotoView.setScaleY((Float) valueAnimator.getAnimatedValue());
            }
        });
        scaleYAnimator.setDuration(300);
        scaleYAnimator.start();

        ValueAnimator scaleXAnimator = ValueAnimator.ofFloat(1, mScaleX);
        scaleXAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                dragPhotoView.setScaleX((Float) valueAnimator.getAnimatedValue());
            }
        });

        scaleXAnimator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animator) {

            }

            @Override
            public void onAnimationEnd(Animator animator) {
                animator.removeAllListeners();
                finish();
                overridePendingTransition(0, 0);
            }

            @Override
            public void onAnimationCancel(Animator animator) {

            }

            @Override
            public void onAnimationRepeat(Animator animator) {

            }
        });
        scaleXAnimator.setDuration(300);
        scaleXAnimator.start();
    }

    private void performEnterAnimation() {
        ValueAnimator translateXAnimator = ValueAnimator.ofFloat(dragPhotoView.getX(), 0);
        translateXAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                dragPhotoView.setX((Float) valueAnimator.getAnimatedValue());
            }
        });
        translateXAnimator.setDuration(300);
        translateXAnimator.start();

        ValueAnimator translateYAnimator = ValueAnimator.ofFloat(dragPhotoView.getY(), 0);
        translateYAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                dragPhotoView.setY((Float) valueAnimator.getAnimatedValue());
            }
        });
        translateYAnimator.setDuration(300);
        translateYAnimator.start();

        ValueAnimator scaleYAnimator = ValueAnimator.ofFloat(mScaleY, 1);
        scaleYAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                dragPhotoView.setScaleY((Float) valueAnimator.getAnimatedValue());
            }
        });
        scaleYAnimator.setDuration(300);
        scaleYAnimator.start();

        ValueAnimator scaleXAnimator = ValueAnimator.ofFloat(mScaleX, 1);
        scaleXAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                dragPhotoView.setScaleX((Float) valueAnimator.getAnimatedValue());
            }
        });
        scaleXAnimator.setDuration(300);
        scaleXAnimator.start();
    }

    @Override
    public void onBackPressed() {
        finishWithAnimation();
    }

    @Override
    public void loadSucceed(String result) {

    }

    @Override
    public void loadFaild(String error) {

    }

//    @Override
//    public boolean onTouchEvent(MotionEvent event) {
//        finish();
//        return super.onTouchEvent(event);
//    }
}
